package com.dao;

import java.util.List;
import java.util.Set;

import com.hbt.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.bean.Factura;
import com.bean.Cliente;

public class FacturaDAO {
	
	private static SessionFactory sf = null;
	private static FacturaDAO instancia = null;
	
	public static FacturaDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new FacturaDAO();
		}
		return instancia;
	}
	
	public void grabarFactura (Factura factura){
		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(factura);
		session.flush();		
		session.getTransaction().commit();	
		session.close();
	}
	
	public void grabarFacturas (Set <Factura> facturas){
		Session session = sf.openSession();
		session.beginTransaction();
		for (Factura f : facturas){
			session.persist(f);
		}
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	public Factura getFactura(Factura fac) {
		Session session = sf.openSession();
		Factura factura = (Factura) session.get(Factura.class, fac.getId());
		session.close();
		return factura;
	}

	public List<Factura> getFacturas() {
		Session session = sf.openSession();
		List<Factura> result = (List<Factura>) session.createQuery("from Factura").list();
		session.close();
		return result;		
	}
	
	
	public Factura getFactura(int id){
		Session session = sf.openSession();
		Factura fac = (Factura) session.createQuery("from Factura f where f.id = :var")
		.setInteger("var", id).setFirstResult(0).setMaxResults(1).uniqueResult();
		session.close();
		return fac;
	}
}
